﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using BKDict.Resources;
using System.IO;
using System.Collections;
using System.Windows.Media.Imaging;
using System.Threading;
using BKDict.Logic;

namespace BKDict
{
    public partial class MainPage : PhoneApplicationPage
    {
        public string kfile = @"MyDict\index.txt";
        public string mfile = @"MyDict\mydict.txt";
        public Dictionary<string, string> Dict = new Dictionary<string, string>();
        public List<string> ListKey;
        public DictionManager dicmanager;
        public FavoriteManager favomanager;
        public RecentlyManager recentlymanager;
        public string Key { get; set; }
        public string Mean { get; set; }
        public int MaxWordInList;

        public MainPage()
        {
            InitializeComponent();
            dicmanager = new DictionManager(mfile, kfile);
            favomanager = new FavoriteManager();
            recentlymanager = new RecentlyManager();
            ListKey = dicmanager.getListKey();
            MaxWordInList = 50;
            SearchKey("a");
            Load_Favorite();
            Load_Recently();
        }

        public void SearchKey(string key)
        {
            lstWords.Items.Clear();
            int i = 0;
            foreach (string e in ListKey)
            {
                string[] words = e.Split('\t');
                if (words[0].StartsWith(key))
                {
                    lstWords.Items.Add(words[0]);
                    Mean = dicmanager.getMean(Convert.ToInt32(words[1]), Convert.ToInt32(words[2]));
                    if (!Dict.ContainsKey(words[0]))
                        Dict.Add(words[0],Mean);
                    i++;
                }
                if (i >= MaxWordInList)
                    break;
            }
        }
        
        private void Image_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                if (tbkey.Text == "")
                    SearchKey("a");
                else
                    SearchKey(tbkey.Text);
            }
            catch (Exception ex)
            {
                tbkey.Text = "";
                MessageBox.Show(ex.Message);
            }
            if (scrMean.Visibility == System.Windows.Visibility.Visible)
            {
                scrMean.Visibility = System.Windows.Visibility.Collapsed;
                imgFavorite.Visibility = System.Windows.Visibility.Collapsed;
            }
            pvtMain.SelectedItem = pvtSearch;
        }

        private void tbkey_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            tbkey.Text = "";
            if (scrMean.Visibility == System.Windows.Visibility.Visible)
            {
                scrMean.Visibility = System.Windows.Visibility.Collapsed;
                imgFavorite.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (lstWords.Visibility == System.Windows.Visibility.Collapsed)
            {
                lstWords.Visibility = System.Windows.Visibility.Visible;
            }
            pvtMain.SelectedItem = pvtSearch;
        }

        private void lstWords_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                Key = lstWords.SelectedItem.ToString();
                if (Key != "")
                {
                    tblMean.Text = "[+] " + Key + "\n" + Dict[Key];
                    tbkey.Text = Key;

                    if (!lstRecently.Items.Contains(Key))
                    {
                        lstRecently.Items.Add(Key);
                        recentlymanager.Write(Key);
                    }

                    if (lstWords.Visibility == System.Windows.Visibility.Visible)
                        lstWords.Visibility = System.Windows.Visibility.Collapsed;

                    if (scrMean.Visibility == System.Windows.Visibility.Collapsed)
                    {
                        scrMean.Visibility = System.Windows.Visibility.Visible;
                        imgFavorite.Visibility = System.Windows.Visibility.Visible;
                        if (lstFavorite.Items.Contains(Key))
                        {
                            Uri uri = new Uri("/Assets/Tiles/Star.png", UriKind.Relative);
                            imgFavorite.Source = new BitmapImage(uri);
                        }
                        else
                        {
                            Uri uri = new Uri("/Assets/Tiles/Star-empty-icon.png", UriKind.Relative);
                            imgFavorite.Source = new BitmapImage(uri);
                        }
                    }
                }
            } catch (Exception) { tbkey.Text = ""; }
        }

        private void tbkey_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (tbkey.Text == "")
                    SearchKey("a");
                else
                    SearchKey(tbkey.Text);
            }
            catch (Exception ex)
            {
                tbkey.Text = "";
                MessageBox.Show(ex.Message);
            }
        }

        private void mnSearch_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            pvtMain.SelectedItem = pvtSearch;
        }

        private void mnRecently_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            pvtMain.SelectedItem = pvtRecently;
        }

        private void mnFavorite_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            pvtMain.SelectedItem = pvtFavorite;
        }

        private void imgFavorite_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (Key != "" && !(lstFavorite.Items.Contains(Key)))
            {
                Uri uri = new Uri("/Assets/Tiles/Star.png", UriKind.Relative);
                imgFavorite.Source = new BitmapImage(uri);
                lstFavorite.Items.Add(Key);
                favomanager.Write(Key);
            }
            else if (Key != "" && (lstFavorite.Items.Contains(Key)))
            {
                Uri uri = new Uri("/Assets/Tiles/Star-empty-icon.png", UriKind.Relative);
                imgFavorite.Source = new BitmapImage(uri);
                lstFavorite.Items.Remove(Key);
            }
        }

        private void Load_Favorite()
        {
            List<string> ListFavo = new List<string>();
            string tmean;
            ListFavo = favomanager.GetList();
            foreach (string e in ListFavo)
            {
                lstFavorite.Items.Add(e);

                //Load mean for this word
                foreach (string tmp in ListKey)
                {
                    string[] words = tmp.Split('\t');
                    if (words[0] == e)
                    {
                        tmean = dicmanager.getMean(Convert.ToInt32(words[1]), Convert.ToInt32(words[2]));
                        if (!Dict.ContainsKey(words[0]))
                            Dict.Add(words[0], tmean);
                    }
                }
            }
        }
        private void Load_Recently()
        {
            List<string> ListRecent = new List<string>();
            string tmean;
            ListRecent = recentlymanager.GetList();
            foreach (string e in ListRecent)
            {
                lstRecently.Items.Add(e);

                //Load mean for this word
                foreach (string tmp in ListKey)
                {
                    string[] words = tmp.Split('\t');
                    if (words[0] == e)
                    {
                        tmean = dicmanager.getMean(Convert.ToInt32(words[1]), Convert.ToInt32(words[2]));
                        if (!Dict.ContainsKey(words[0]))
                            Dict.Add(words[0], tmean);
                    }
                }
            }
        }
        private void imgHome_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            pvtMain.SelectedItem = pvtHome;
        }

        private void imgSearch1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            pvtMain.SelectedItem = pvtSearch;
        }

        private void imgRecently_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            pvtMain.SelectedItem = pvtRecently;
        }

        private void imgFavorite1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            pvtMain.SelectedItem = pvtFavorite;
        }

        private void lstFavorite_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                Key = lstFavorite.SelectedItem.ToString();
                if (Key != "")
                {
                    tblMean.Text = "[+] " + Key + "\n" + Dict[Key];
                    tbkey.Text = Key;

                    if (!lstRecently.Items.Contains(Key))
                    {
                        lstRecently.Items.Add(Key);
                        recentlymanager.Write(Key);
                    }

                    if (lstWords.Visibility == System.Windows.Visibility.Visible)
                        lstWords.Visibility = System.Windows.Visibility.Collapsed;

                    if (scrMean.Visibility == System.Windows.Visibility.Collapsed)
                    {
                        scrMean.Visibility = System.Windows.Visibility.Visible;
                        imgFavorite.Visibility = System.Windows.Visibility.Visible;
                        if (lstFavorite.Items.Contains(Key))
                        {
                            Uri uri = new Uri("/Assets/Tiles/Star.png", UriKind.Relative);
                            imgFavorite.Source = new BitmapImage(uri);
                        }
                        else
                        {
                            Uri uri = new Uri("/Assets/Tiles/Star-empty-icon.png", UriKind.Relative);
                            imgFavorite.Source = new BitmapImage(uri);
                        }
                    }
                }
                pvtMain.SelectedItem = pvtSearch;
            }
            catch (Exception) { tbkey.Text = ""; }
        }

        private void lstRecently_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                Key = lstRecently.SelectedItem.ToString();
                if (Key != "")
                {
                    tblMean.Text = "[+] " + Key + "\n" + Dict[Key];
                    tbkey.Text = Key;

                    if (!lstRecently.Items.Contains(Key))
                    {
                        lstRecently.Items.Add(Key);
                        recentlymanager.Write(Key);
                    }

                    if (lstWords.Visibility == System.Windows.Visibility.Visible)
                        lstWords.Visibility = System.Windows.Visibility.Collapsed;

                    if (scrMean.Visibility == System.Windows.Visibility.Collapsed)
                    {
                        scrMean.Visibility = System.Windows.Visibility.Visible;
                        imgFavorite.Visibility = System.Windows.Visibility.Visible;
                        if (lstFavorite.Items.Contains(Key))
                        {
                            Uri uri = new Uri("/Assets/Tiles/Star.png", UriKind.Relative);
                            imgFavorite.Source = new BitmapImage(uri);
                        }
                        else
                        {
                            Uri uri = new Uri("/Assets/Tiles/Star-empty-icon.png", UriKind.Relative);
                            imgFavorite.Source = new BitmapImage(uri);
                        }
                    }
                }
                pvtMain.SelectedItem = pvtSearch;
            }
            catch (Exception) { tbkey.Text = ""; }
        }


    }
}